package com.shanjupay.paymentagent.message;

import com.alibaba.fastjson.JSON;
import com.shanjupay.paymentagent.api.dto.PaymentResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @className: PayProducer
 * @Description: TODO
 * @author: Huangjianyun
 * @date: 2021/10/28 12:06
 */
@Slf4j
@Component
public class PayProducer {
    //主题
    private static final String TOPIC_ORDER = "TP_PAYMENT_ORDER";
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    //同步延迟发送消息
    public void payOrderNotice(PaymentResponseDTO result){

        log.info("支付通知发送延迟消息：{}",result);
        try {
            //封装消息
            Message<PaymentResponseDTO> message = MessageBuilder.withPayload(result).build();
            //rocketMQTemplate模板进行同步延迟的发送消息
            rocketMQTemplate.syncSend(TOPIC_ORDER,message,1000,3);
        }catch (Exception e){
            log.warn(e.getMessage(),e);
        }
    }
    //将查询到订单结果发送RocketMQ
    //1构造一个主题，专门用来发送查询到的定的结果的消息
    private static final String TOPIC_RESULT = "TP_PAYMENT_RESULT";
    public void payResultNotice(PaymentResponseDTO paymentResponseDTO){
        rocketMQTemplate.convertAndSend(TOPIC_RESULT,paymentResponseDTO);
        //打个日志
        log.info("支付渠道代理服务向mq支付结果消息： {}", JSON.toJSONString(paymentResponseDTO));
    }















}
